﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using NextCMS.Models;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class SiteSettings : ISiteSettings
    {
        private static string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/SiteSettings.xml";
        XDocument doc = null;
        public SiteSettings()
        {
            doc = XDocument.Load(FilePath);  //load the xml file.
        }
        public IEnumerable<SiteSettingsModel> GetAll()
        {
            var q = from d in doc.Descendants("SiteSetting")
                    select new SiteSettingsModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        Key = d.Element("Key").Value,
                        Value = d.Element("Value").Value
                    };
            return q;
        }
        
        public string GetByKey(string Key)
        {
            
            var q = from d in doc.Descendants("SiteSetting")
                    where d.Element("Key").Value == Key && (int)d.Element("SiteId") == 0
                    select new
                    {
                        Value = d.Element("Value").Value
                    };
            if (q.FirstOrDefault() == null)
                return "";
            else
                return q.FirstOrDefault().Value;
        }
        public string GetByKey(string Key,int SiteId)
        {

            var q = from d in doc.Descendants("SiteSetting")
                    where d.Element("Key").Value == Key && (int)d.Element("SiteId")==SiteId
                    select new
                    {
                        Value = d.Element("Value").Value
                    };
            if (q.FirstOrDefault() == null)
                return "";
            else
                return q.FirstOrDefault().Value;
        }
        public int Save(SiteSettingsModel page)
        {
            
            var q = from d in doc.Descendants("SiteSetting")
                    select new SiteSettingsModel
                    {
                        Id = (int)d.Element("Id"),
                        Key = d.Element("Key").Value,
                        Value = d.Element("Value").Value
                    };
            if (q.Where(c => c.Key == page.Key && c.SiteId==page.SiteId).FirstOrDefault() != null)
                return 0;

            if (q.Count() > 0)
                page.Id = q.Max(s => s.Id) + 1;
            else
                page.Id = 1;


            var oMember = new XElement("SiteSetting",
             new XElement("Id", page.Id),
             new XElement("SiteId", page.SiteId),
             new XElement("Key", page.Key),
             new XElement("Value", page.Value)
             );

            doc.Root.Add(oMember);
            doc.Save(FilePath);
            return page.Id;
        }
        // Delete Record
        public void Delete(int id)
        {
            
            doc.Root.Elements("SiteSetting").Where(i => (int)i.Element("Id") == id).Remove();
            doc.Save(FilePath);
        }

        // Edit Record
        public void Edit(string Key, string Value,int SiteId)
        {

            XElement node = doc.Root.Elements("SiteSetting").Where(i => i.Element("Key").Value == Key && (int)i.Element("SiteId") == SiteId).FirstOrDefault();
            if (node == null)
            {
                SiteSettingsModel sm = new SiteSettingsModel();
                sm.Key = Key;
                sm.Value = Value;
                sm.SiteId = SiteId;
                Save(sm);
            }
            else
            {
                node.SetElementValue("Value", Value);
                doc.Save(FilePath);
            }
        }

        public void Dispose()
        {
            doc = null;
        }
    }
}
